$(function () {
  //定义美化时间的过滤器
  template.defaults.imports.formatDate = function (date) {//art-template日期格式化
    date = new Date(date)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    return year + '-' + month + '-' + day
  } 

  function addComment() {
    $.ajax({
      type:'GET',
      url:'/admin/comments',
      success:function (res) {
        let tags = template('table-tpl', res)
        $('.layui-table tbody').html(tags) 
      }
      
    })
  }
  addComment()

  // 删除
  $('.layui-table tbody').on('click', '.delete', function (e) {
    var id = $(e.target).data('id')
    layer.confirm('确认要删除吗？', function (index) {
      $.ajax({
        type: 'DELETE',
        url: '/admin/comments/' + id,
        success: function (res) {
          if (res.status === 0) {
            layer.close(index)
            addComment()
          }
        }
      })
    })
  })

})
